package com.vikutorika.servlet;

import com.vikutorika.dao.imp.UserInfoDAOImp;
import com.vikutorika.service.MessageService;
import com.vikutorika.util.CookieUtil;
import com.vikutorika.vo.ResultVo;
import com.vikutorika.entity.UserInfo;
import com.vikutorika.service.UserService;
import com.vikutorika.util.ParamUtil;
import com.vikutorika.vo.UnreadMessageEventVo;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@WebServlet("/user")
public class UserServlet extends NewBaseServlet {
    public ResultVo login(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        UserInfo userInfo = ParamUtil.paramToBean(req, UserInfo.class);
        ResultVo resultVo = new UserService().login(userInfo);
        if (resultVo.getCode().equals("200")) {
            UserInfo data = (UserInfo) resultVo.getData();

            // 创建未读消息推送事件对象
            ConcurrentHashMap<String, UnreadMessageEventVo> map = new ConcurrentHashMap<>();
            PrivateChatServlet.msgs.put(data.getUid(),map);

            ServletContext servletContext = req.getServletContext();
            servletContext.setAttribute(data.getUid().toString(), "online");
            HttpSession session = req.getSession();
            session.setAttribute("userInfo", resultVo.getData());
            String autoLogin = req.getParameter("autoLogin");
            String remember = req.getParameter("remember");
            if (autoLogin != null && autoLogin.equals("true"))
                CookieUtil.sendCookie(resp, req.getContextPath(), "autoLogin", "true");
            else
                CookieUtil.removeCookie(resp, req.getContextPath(), "autoLogin");
            if (remember != null && remember.equals("true")) {
                CookieUtil.sendCookie(resp, req.getContextPath(), "phone", userInfo.getPhone());
                CookieUtil.sendCookie(resp, req.getContextPath(), "password", userInfo.getPassword_hash());
            } else {
                CookieUtil.removeCookie(resp, "/login.html", "phone");
                CookieUtil.removeCookie(resp, "/login.html", "password");
            }
        }
        return resultVo;
    }
    public ResultVo enroll(HttpServletRequest req, HttpServletResponse resp) {
        UserInfo userInfo = ParamUtil.paramToBean(req, UserInfo.class);
        ResultVo resultVo = new UserService().enroll(userInfo);
        return resultVo;
    }
    public ResultVo logout(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        ServletContext servletContext = req.getServletContext();
        UserInfo user = (UserInfo) session.getAttribute("userInfo");
        if (user != null) {
            session.removeAttribute("userInfo");
            if (servletContext.getAttribute(user.getUid().toString()) != null)
                servletContext.removeAttribute(user.getUid().toString());
            return ResultVo.success("登出成功");
        }
        return ResultVo.success();
    }
    public ResultVo getInfo(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        Object user = session.getAttribute("userInfo");
        return ResultVo.success(user);
    }
}
